<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addRoleVisble=true">新增角色</el-button>
            <el-table
              :data="tableData"
              style="width: 100%; margin-top:15px ;"
              border
            >
              <el-table-column
                label="序号"
                type="index"
                width="60"
              />
              <el-table-column
                prop="name"
                label="角色名"
                sortable
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)"
                  >
                    分配权限
                  </el-button>
                  <el-button
                    type="text"
                    size="mini"
                    @click="handleDelete(scope.row)"
                  >
                    修改
                  </el-button>
                  <el-button
                    type="text"
                    size="mini"
                    @click="handleJurisdiction(scope.$index, scope.row)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-row>
              <el-col :offset="3" :span="8">
                <el-form :model="company" label-width="80px" disabled>
                  <el-form-item label="企业名称">
                    <el-input v-model="company.name" />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <el-pagination
          :current-page="queryData.page"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
    </div>
    <!-- 弹出层 -->
    <el-dialog
      :title="addRoleForm.id ?'编辑角色':'新增角色'"
      :visible.sync="addRoleVisble"
      width="30%"
      @close="addDialogClose"
    >
      <el-form ref="addRoleForm" :model="addRoleForm" :rules="addRoleRules" label-width="80px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addRoleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleVisble = false">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getUserCompany } from '@/api/company.js'
import { getRolesList, postRolesList, putRolesList } from '@/api/roles.js'
export default {
  data() {
    return {
      activeName: 'first',
      company: {},
      tableData: [],
      queryData: {
        page: 1,
        pagesize: 2
      },
      // 一共几条数据
      total: 0,
      addRoleVisble: false,
      addRoleForm: {
        name: '',
        description: ''
      },
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 28, message: '长度在 2 到 28 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.userCompany()
    this.rolesList()
  },

  methods: {
    addDialogClose() {
      this.$refs.addRoleForm.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    },
    // 获取公司信息
    async userCompany() {
      const res = await getUserCompany(this.companyId)
      console.log(res)
      this.company = res
    },
    // 获取角色信息
    async rolesList() {
      const res = await getRolesList(this.queryData)
      console.log(res)
      this.tableData = res.rows
      this.total = res.total
    },
    // 分页点击事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryData.pagesize = val
      this.rolesList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryData.page = val
      this.rolesList()
    },
    // 确定提交添加
    async onSave() {
      this.$refs.addRoleForm.validate()
      try {
        if (this.addRoleForm.id) {
          await putRolesList(this.addRoleForm)
          this.$message.success('修改成功')
        } else {
          await postRolesList(this.addRoleForm)
          this.$message.success('添加成功')
        }
        this.addRoleVisble = false
        this.rolesList()
      } catch (err) {
        console.log(err)
      }
    },
    // 修改表单
    handleDelete(row) {
      this.addRoleVisble = true
      this.addRoleForm = { ...row }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-tabs__header {
  .el-tabs__item {
    width: 158px;
    font-size: 18px;
    font-weight: 700;
    text-align: center;

  }
}
</style>
